package com.example.studentgradesmanagement.controller;

import com.example.studentgradesmanagement.model.Grade;
import com.example.studentgradesmanagement.model.Student;
import com.example.studentgradesmanagement.service.StudentService;
import com.example.studentgradesmanagement.util.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/listAll")
    public ResponseEntity<List<Student>> listAll() {
        List<Student> students = studentService.listAll();
        return ResponseEntity.ok(students);
    }

    @GetMapping("/getById/{studentId}")
    public ResponseEntity<ApiResponse> getById(@PathVariable Long studentId) {
        Optional<Student> student = studentService.getById(studentId);
        if (student.isPresent()) {
            return ResponseEntity.ok(new ApiResponse(true, "Student found", student.get()));
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping("/getGrades/{studentId}")
    public ResponseEntity<ApiResponse> getGrades(@PathVariable Long studentId) {
        List<Grade> grades = studentService.getGrades(studentId);
        if (grades != null) {
            return ResponseEntity.ok(new ApiResponse(true, "Grades found", grades));
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @PostMapping("/add")
    public ResponseEntity<ApiResponse> add(@RequestBody Student student) {
        studentService.add(student);
        return ResponseEntity.ok(new ApiResponse(true, "Student added successfully", student));
    }
}
